A Step-by-Step Guide to Building an Application with Flex
Table of Contents
Code Walkthrough
You build Flex applications with MXML, a tag-based, declarative language for laying out the user interface and ActionScript script for procedural logic. Read An Overview of MXML, the Macromedia Flex Markup Language, for more information. The code required to build the Flex Store application is organized similarly to how you would architect a typical enterprise application, partitioning the application using a model, view, controller design pattern.
The following is the class diagram describing the application:

Figure 1. A UML describes the application.
Creating a Flex application is just like creating a Java, .NET or any other object-oriented application—you create and assemble classes. The Flex class diagram shows three types of classes with user interface (view) classes implemented in MXML and the models in Action Script. The controller classes are the web services and bindings that are part of the data management capabilities in Flex.
To see the files associated with the Flex Store application, browse to default Flex server install\samples on your installation or in the files you downloaded in the Requirements section at the beginning of this article. Open the flexstore.mxml file in your favorite text editor or IDE.
Encapsulation and Reuse
The flexstore.mxml file is the main class of the application and aggregates the different views available in the application: ThumbnailView, GridView, and CartView. Because they are user interface classes, they are also defined in MXML. You implement the views as classes and when you create a class in Flex, you are able to reference it as a tag. In this case, the user can see the same data displayed as a series of thumbnail graphics or in a data grid. The view classes reference the same data model but do not include the data. This lets you to partition the application for more maintainable code.
With Flex, developers can encapsulate functionality and reference it as tags; this is a real boon to reuse and team development. Developers assembling the application don’t have to know how the component was created; they simply reference it as a tag.
Event Model
To provide loose coupling and reusability, Flex enables component communication through an event model that will be familiar to Java and .NET programmers. The event model allows components to register as listeners to events so that they are notified when that event is triggered. An example this is in flexstore.mxml where it registers itemSelected and checkOut for CartView.mxml. Because of this registration, you don’t have to hard code the display of the checkout view, instead you dispatch a checkout event and the listener components react appropriately.
Data Management
You access dynamic data in three different ways: through web services, as a call to a remote Java object, or as XML over HTTP. The Flex Store application shows an example of a web service invocation where the Flex web service tag does all of the plumbing. It takes care of the serialization and deserialization of data as well as the service brokering. You define your web service declaratively by pointing at the WSDL file and Flex does the rest.
Flex’s data binding allows you to bind the property of one component to the property of another component. You typically use it in a data retrieval scenario where the property of a user interface component is bound to a property of a model component (maintaining good separation of model and view) while letting you write a minimal amount of code to display data in user interface components. The flexstore.mxml application file defines a model named catalog where the catalog model is bound to the result of the web service invocation. The thumbnail view and grid view are then both bound to the catalog model with the curly brace binding syntax, a very readable and elegant means of binding.